package com.samsung.spen.sshare.main.sms;

import com.samsung.spen.sshare.R;
import com.samsung.spen.sshare.main.MainActivity;
import com.samsung.spen.sshare.main.data.SshareDataService;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.Builder;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;

public class SMSReceiver extends BroadcastReceiver implements Constants {

	private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
	public static final String SMS_RECEIVER_INSERT = "SMS_RECEIVER_INSERT";
	public static final String PHONE = "phone";
	public static final String TIME = "time";
	public static final String CONTENT = "content";
	public static final String IP = "ip";

	@Override
	public void onReceive(Context context, Intent intent) {
		if (intent.getAction().equals(SMS_RECEIVED)) {
			Bundle bundle = intent.getExtras();
			Log.d(this.getClass().toString(), "SMS received");
			Toast.makeText(context, "sms received", Toast.LENGTH_SHORT).show();
			Object[] pdus = (Object[]) bundle.get("pdus");
			SmsMessage[] messages = new SmsMessage[pdus.length];
			for (int i = 0; i < pdus.length; i++) {
				messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
			}

			SmsMessage sms = messages[0];
			String body = "";
			String phoneNum = "";
			long time = 0;
			String ip;
			String content;
			
			phoneNum = messages[0].getDisplayOriginatingAddress();
			time = messages[0].getTimestampMillis();
			try {
				if (messages.length == 1 || sms.isReplace()) {
					body = sms.getDisplayMessageBody();
				} else {
					StringBuilder bodyText = new StringBuilder();
					for (int i = 0; i < messages.length; i++) {
						bodyText.append(messages[i].getMessageBody());
					}
					body = bodyText.toString();
				}
			} catch (Exception e) {

			}
			
			if(body.contains(TOKEN_SEPARATED))
			{
				String[] tokens = body.split(TOKEN_SEPARATED);
				if(tokens[0].equals(INDENTIFICATION_STRING)){
					//Handle sms
					this.abortBroadcast();
					
					//Handle sms
					ip = tokens[1];
					content = tokens[2];
					
					Intent i = new Intent(context, SshareDataService.class);
					i.setAction(SMS_RECEIVER_INSERT);
					i.putExtra(PHONE, phoneNum);
					i.putExtra(TIME, time);
					i.putExtra(CONTENT, content);
					i.putExtra(IP, ip);
					context.startService(i);
					
					//fire notification 
					NotificationCompat.Builder builder = new Builder(context);
					builder.setSmallIcon(R.drawable.ic_launcher);
					builder.setContentTitle("Request From : " + phoneNum);
					builder.setContentText(content);
					NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
					Notification notification = builder.build();
					
					Intent intentNtf = new Intent(context, MainActivity.class);
					intentNtf.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
					PendingIntent pi = PendingIntent.getActivity(context, 0, intentNtf, 0);
					notification.contentIntent = pi;
					notification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;

					mNotificationManager.notify((int)System.currentTimeMillis(), notification);
					context.stopService(i);
				}
			}

			Log.d(this.getClass().toString(), body);
		}
	}

}
